package Core;
/*
 *  J2Egn version 2.6.28
 *  Author: Van Tan Tai 2010-2011
 *  all for KEY
 */
import Core.GLives.VENDOR;

public class KEY {
	public static final short NUM0 = 0 ;
	public static final short NUM1 = 1 ;
	public static final short NUM2 = 2 ;
	public static final short NUM3 = 3 ;
	public static final short NUM4 = 4 ;
	public static final short NUM5 = 5 ;
	public static final short NUM6 = 6 ;
	public static final short NUM7 = 7 ;
	public static final short NUM8 = 8 ;
	public static final short NUM9 = 9 ;
	public static final short POUND = 10 ;
	public static final short STAR = 11 ;
	public static final short UP = 12 ;
	public static final short DOWN = 13;
	public static final short LEFT = 14 ;
	public static final short RIGHT = 15 ;
	public static final short CLEAR = 16 ;
	public static final short BACK = 17 ;
	public static final short FIRE = 18 ;
	public static final short OK = 18 ;
	public static final short LEFT_SOFTKEY = 19;
	public static final short RIGHT_SOFTKEY = 20;
	public static final short KEYCOUNT = 21;
	
	public static final short INVALID_KEY = -1;
	
	public static final short EVENT_PRESS = -2;
	public static final short EVENT_RELEASE = -3;
	
	public static char AsciiKeyPressed;
	public static int LastCode = -43;
	public static int LastKey = -43;
	public static short Keys[] = new short[KEYCOUNT];

	public static int getKey(int keyCode, short event)
    {
		int k = INVALID_KEY;
		
		LastCode = keyCode;
		AsciiKeyPressed = keyCode >= 32 ? (char)keyCode : 0;

		if (GLives.vendorDevice == VENDOR.MOTOROLA)
            keyCode = motorolaKeys(keyCode);
        else if (GLives.vendorDevice == VENDOR.BENQ)
			keyCode = benqKeys(keyCode);
        else if (GLives.vendorDevice == VENDOR.BLACKBERRY)
        {
			if (keyCode == 113 || keyCode == 35 || keyCode == 81)
				keyCode = -6;
			else if (keyCode == 112 || keyCode == 64 || keyCode == 80)
				keyCode = -7;
		}
		
		if (GLives.ins.isGameKey)
			keyCode = convertToGameKey(keyCode);
		
		switch (keyCode)
        {
            case 48: case 49: case 50: case 51: // key[0-9]
            case 52:case 53:case 54:case 55:case 56:
            case 57: k = keyCode - 48;		break;
            case -5:case 10:case 13:k=FIRE;	break;
            case -1: case 1: k = UP;		break;
            case -2: case 6: k = DOWN;		break;
            case -3: case 2: k = LEFT;		break;
            case -4: case 5: k = RIGHT;		break;
            case -8: case 8: k = CLEAR;		break;
            case 42: k = STAR;				break; // Key [*]
            case 35: k = POUND;				break; // Key [#]
            case -6: k = LEFT_SOFTKEY;		break;
            case -7: k = RIGHT_SOFTKEY;		break;
            case -11: k = BACK;				break;
            //default: return keyCode;
		}
		if(k != INVALID_KEY) Keys[k] = (short)(event == EVENT_PRESS ? 1 : 0);
		LastKey = (short)(event == EVENT_PRESS ? k : INVALID_KEY);
		return k;
	}

	private static int motorolaKeys(int key)
    {
		int keyCode = key;

		switch (keyCode)
        {
            case -21:keyCode = -6;break;
            case -22:keyCode = -7;break;
            case -23:keyCode = -8;break;
            case -2:keyCode = -3;break;
            case -5:keyCode = -4;break;
            case -6:keyCode = -2;break;
            case -20:keyCode = -5;break;
		}
		return keyCode;
	}

	private static int benqKeys(int key)
    {
		int keyCode = key;
		switch (keyCode)
        {
            case -59:keyCode = -1;break;
            case -60:keyCode = -2;break;
            case -61:keyCode = -3;break;
            case -62:keyCode = -4;break;
            case -26:keyCode = -5;break;
            case -1:keyCode = -6;break;
            case -4:keyCode = -7;break;
		}
		return keyCode;
	}

	private static int convertToGameKey(int key)
    {
		int keyCode = key;
		
		if(GLives.isQwerty){
			int ascii = (AsciiKeyPressed >= 97 && AsciiKeyPressed <= 122 ? AsciiKeyPressed - 32 : AsciiKeyPressed);
			if (GLives.vendorDevice == VENDOR.FPT){
				switch (ascii){
	            case 'E':keyCode = -1;break; // key up
	            case 'X':keyCode = -2;break; // key down
	            case 'S':keyCode = -3;break; // key left
	            case 'F':keyCode = -4;break; // key right
	            case 'D':keyCode = -5;break; // key fire
				}
			} else if (GLives.vendorDevice == VENDOR.NOKIA){
				switch (ascii){
	            case 'T':keyCode = -1;break;
	            case 'B':keyCode = -2;break;
	            case 'F':keyCode = -3;break;
	            case 'H':keyCode = -4;break;
	            case 'G':keyCode = -5;break;
				}
			} else if (GLives.vendorDevice == VENDOR.SAMSUNG){
				switch (ascii){
	            case 'R':keyCode = -1;break;
	            case 'C':keyCode = -2;break;
	            case 'D':keyCode = -3;break;
	            case 'G':keyCode = -4;break;
	            case 'F':keyCode = -5;break;
				}
			}
		}
		switch (key){
            case 50:keyCode = -1;break; // key [2]
            case 56:keyCode = -2;break; // key [8]
            case 52:keyCode = -3;break; // key [4]
            case 54:keyCode = -4;break; // key [6]
            case 53:keyCode = -5;break; // key [5]
		}
		return keyCode;
	}
	
	public static boolean isPressed(short keyName){
		return Keys[keyName] > 0;
	}
	
	public static int LoopCount(short keyName){
		return Keys[keyName];
	}
	
	public static void resetKeys(){
		for(int i = 0; i < KEYCOUNT; i++)
			Keys[i] = 0;
	}
	public static void resetKey(short keyName){
		Keys[keyName] = 0;
	}
	public static void pressKey(int keyCode){
		if(keyCode >= 0 && keyCode < KEYCOUNT){
			if(Keys[keyCode] == 0) Keys[keyCode] = 1;
		} else getKey(keyCode, EVENT_PRESS);
	}
	public static void update(){
		for(int i = 0; i < KEYCOUNT; i++){
			if(Keys[i] > 0 && Keys[i] < Short.MAX_VALUE){
				Keys[i]++;
			}
		}
	}
}
